package com.csweimei.rentmanagementtreasure.viewModel;

import android.app.Application;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;

import com.csweimei.rentmanagementtreasure.LoginActivity;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.CountBean;
import com.csweimei.rentmanagementtreasure.http.entity.CustomerBean;
import com.csweimei.rentmanagementtreasure.http.entity.HouseListBean;
import com.csweimei.rentmanagementtreasure.http.entity.VipBean;
import com.csweimei.rentmanagementtreasure.pop.PopHomeAdd;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.CustomerItemVM;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.CustomerVm;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.HouseInfoPVM;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.HouseInfoVM;
import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.lxj.xpopup.XPopup;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.internal.observers.DisposableLambdaObserver;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class MainVm extends BaseViewModel<Repository> {

    public MainVm(@NonNull Application application, Repository model) {
        super(application, model);
    }

    @Override
    public void onResume() {
        super.onResume();

        if (model.isLogin()){
            getUserData();
            getVipInfo();
            getData();
        }
    }

    int cusNum = 0;
    int ZzNum = 0;
    int  HzNum = 0;


    private void getData() {
        HashMap<String,String> map = new HashMap<>();
        map.put("user_id",model.getLoginInfo().getData().getUser_id());
        model.housing_item(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<CountBean>() {
                    @Override
                    public void onNext(CountBean o) {
                        if (o.issucc){
                            HzNum = o.getData().getHcount();
                            ZzNum = o.getData().getZcount();
                            cusNum = o.getData().getCcount();
                            SpUtils.getInstance().putInt("cusNum",cusNum);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getVipInfo() {
        model.seach_vip(AppConfig.getCurMap(model))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<VipBean>() {
                    @Override
                    public void onNext(VipBean o) {
                        if (o.issucc){
                            AppConfig.setVipPerson(o.getData().getDay());
                            AppConfig.setVipEnt(o.getData().getCompanyDay());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getUserData() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        model.userinfo(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver() {
                    @Override
                    public void onNext(Object o) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public BindingCommand addPop = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                PopHomeAdd popHomeAdd = new PopHomeAdd(mActivity);
                PopUtils.showPop(mActivity,popHomeAdd,false);
                popHomeAdd.setHouseNum(ZzNum);
                popHomeAdd.setZyNum(HzNum);
                popHomeAdd.setCusNum(cusNum);
            }else {
                ToastUtils.showShort("请先登录");
                mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
            }


        }
    });
}
